package Module::Changes::Parser::YAML;

use warnings;
use strict;
use YAML;
use Module::Changes;
use DateTime::Format::W3CDTF;
use Perl::Version;


our $VERSION = '0.06';


use base 'Module::Changes::Parser';


sub parse_string {
    my ($self, $content) = @_;
    my $spec = Load($content);

    my $changes = Module::Changes->make_object_for_type('entire');

    die "no 'global' section\n" unless exists $spec->{global};
    die "no 'global:name' value\n" unless exists $spec->{global}{name};

    $changes->name($spec->{global}{name});
    for my $rel_spec (@{ $spec->{releases} || []}) {
        $changes->releases_push(Module::Changes
            ->make_object_for_type('release',
            version => Perl::Version->new($rel_spec->{version}),
            date    => DateTime::Format::W3CDTF->new->parse_datetime(
                        $rel_spec->{date}),
            author  => $rel_spec->{author},
            changes => $rel_spec->{changes},
            tags    => $rel_spec->{tags},
        ));
    }

    $changes;
}


1;


__END__

{% USE p = PodGenerated %}

=head1 NAME

Module::Changes::Parser::YAML - Parse a YAML Changes file into objects

=head1 SYNOPSIS

    use Module::Changes;
    my $parser = Module::Changes->make_object_for_type('parser_yaml');
    my $changes = $parser->parse_from_file('Changes');

=head1 DESCRIPTION

This class can parse a YAML Changes file or string and return an object
hierarchy representing the Changes.

=head1 METHODS

=over 4

{% p.write_methods %}

=item parse_string

    $parser->parse_string($yaml_string);

Takes a string containing YAML and returns a Changes object that contains all
the information about releases and so on.

=back

{% p.write_inheritance %}

{% PROCESS standard_pod %}

=cut

